

public class TASK3 {

    public static void main(String[] args) {

        Tickets tickets = new Tickets();

        Thread thread1 = new Thread(tickets, "窗口1");

        thread1.start();

        Thread thread2 = new Thread(tickets, "窗口2");

        thread2.start();

        Thread thread3 = new Thread(tickets, "窗口3");

        thread3.start();

    }

}

class Tickets implements Runnable {

    static int countTickets = 100;

    @Override

    public void run() {

        while (true) {

            synchronized (this) {

                if (countTickets > 0) {

                    try {

                        Thread.sleep(10);

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                    System.out.println(Thread.currentThread().getName() + "卖票，票号是：" + countTickets);

                    countTickets--;

                } else {

                    break;

                }

            }

        }

    }

}